numCourses=4
prerequisites=[[2,0],[1,0],[3,1],[3,2],[1,3]]
def canFinish(numCourses: int, prerequisites) -> bool:
    dictionary=dict()
    #先把所有的都初始化为0
    for i in range(numCourses):
        dictionary[i]=0
    for i in prerequisites:
        dictionary[i[0]]+=1
    print(dictionary)
    courseF=set()
    for i in range(numCourses):
        if dictionary[i]==0:
            #如果存在没有被指向的节点
            courseF.add(i)
    print(courseF)
    flag=True
    while flag and len(prerequisites)>0:
        flag=False
        prerequisitesLen=len(prerequisites)
        while prerequisitesLen>0:
            prerequisite=prerequisites.pop(0)
            if prerequisite[1] in courseF:
                dictionary[prerequisite[0]]-=1
                if dictionary[prerequisite[0]]<=0 and prerequisite[0] not in courseF:
                    courseF.add(prerequisite[0])
                    flag=True
            else:
                prerequisites.append(prerequisite)
            prerequisitesLen-=1
        print(courseF)
    if len(courseF)==len(dictionary):
        return True
    else:
        return False
print(canFinish(numCourses,prerequisites))








